<?php 
/*
parameters:
	(opt) string host
	(opt) user
	(opt) pass
	(opt) dbName
methods:
	getAllDna()
		returns array of dna arrays (key, string, pivot distances)
	getDnaByKey($key)
		returns single dna array 
	insertPlainDna($string)
		inserts dna string to database
*/
Class DnaDb {
	
	private $conn = null;
	/* LOCALHOST */
	function __construct($host="localhost", $user="root", $pass="root", $dbName="cvut-vmw-dna-align"){
		$this->conn = mysql_connect($host, $user, $pass) or die("Could not access to database");
		mysql_select_db($dbName) or die("Could not connect to database");
		mysql_set_charset('utf8',$this->conn); 
	}
	/* OUT
	function __construct($host=":/var/run/mysqld/mysqld.sock", $user="slacadav", $pass="pmqub965", $dbName="slacadav"){
		$this->conn = mysql_connect($host, $user, $pass) or die("Could not access to database");
		mysql_select_db($dbName) or die("Could not connect to database");
		mysql_set_charset('utf8',$this->conn); 
	} */
	
	public function getAllDna() {
		$query = mysql_query("SELECT * FROM dna");
		return $this->getAll($query);
	}
	
	public function getDnaByKey($key) {
		$query = mysql_query("SELECT * FROM dna WHERE dnaKey =".$key);
		$toReturn = $this->getAll($query);
		return $toReturn[0];
		
	}
	
	public function insertPlainDna($string) {
		$query = "INSERT INTO dna (dnaString) VALUES ('".$string."')";
		$return = mysql_query($query);
	}
	
	private function getAll($query){
		$toReturn = array();
		while ($row = mysql_fetch_assoc($query)) {
			array_push($toReturn, $row);
		}
		return $toReturn;
	}
}
?>
